import java.util.Map;
import java.util.TreeMap;

/**
 * @author hewei
 * @version 1.0
 * @description: 周赛307 t2
 * @date 2022/8/21 10:37
 */

public class LargestPalindromic {

    public static void main(String[] args) {
        LargestPalindromic largestPalindromic = new LargestPalindromic();
        System.out.println(largestPalindromic.largestPalindromic("00011"));
    }

    public String largestPalindromic1(String num) {
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < num.length(); i++) {
            int temp = num.charAt(i) - '0';
            map.put(temp, map.getOrDefault(temp, 0) + 1);
        }
        StringBuilder stringBuilder = new StringBuilder();
        boolean zero = false;
        int flag = 10;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            if (key != 0) {
                for (int i = 0; i < value / 2; i++) {
                    stringBuilder.append(key);
                    stringBuilder.insert(0, key);
                }
                if (value % 2 == 1) flag = key;
            } else zero = true;
        }
        int count = map.getOrDefault(0, 0);
        for (int i = 0; i < count / 2 && stringBuilder.length() >= 2; i++) {
            stringBuilder.insert(stringBuilder.length() / 2, 0);
            stringBuilder.insert(stringBuilder.length() / 2, 0);
        }
        if (zero || flag != 10) {
            if (flag != 10) {
                stringBuilder.insert(stringBuilder.length() / 2, flag);
            } else stringBuilder.insert(stringBuilder.length() / 2, 0);
        }
        return stringBuilder.toString();
    }

    public String largestPalindromic(String num) {
        int[] nums = new int[10];
        for (int i = 0; i < num.length(); i++) {
            nums[num.charAt(i) - '0']++;
        }
        StringBuilder stringBuilder = new StringBuilder();
        int flag = 10;
        for (int i = 9; i >= 0; i--) {
            int count = nums[i];
            if (i != 0 || stringBuilder.length() > 0)
                for (int j = 0; j < count / 2; j++) {
                    stringBuilder.append(i);
                    nums[i] -= 2;
                }
            if (nums[i] == 1 && flag == 10) flag = i;
        }
        String s = stringBuilder.toString();
        s += stringBuilder.reverse().toString();
        stringBuilder = new StringBuilder(s);
        if (flag != 10) {
            stringBuilder.insert(stringBuilder.length() / 2, flag);
        } else if (nums[0] != 0) {
            stringBuilder.insert(stringBuilder.length() / 2, 0);
        }
        return stringBuilder.toString();
    }
}
